@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: PBJ Server Windows 快速启动脚本

:: 颜色定义
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

:: 打印带颜色的消息
:print_message
echo %GREEN%[INFO]%NC% %~1
goto :eof

:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof

:print_header
echo %BLUE%================================%NC%
echo %BLUE%    PBJ Server 快速启动脚本    %NC%
echo %BLUE%================================%NC%
goto :eof

:: 检查依赖
:check_dependencies
call :print_message "检查依赖..."

:: 检查 Go
go version >nul 2>&1
if errorlevel 1 (
    call :print_error "Go 未安装，请先安装 Go 1.21+"
    exit /b 1
)

:: 检查 Go 版本
for /f "tokens=3" %%i in ('go version') do set GO_VERSION=%%i
set GO_VERSION=!GO_VERSION:go=!
call :print_message "Go 版本检查通过: !GO_VERSION!"

:: 检查 MySQL（可选）
mysql --version >nul 2>&1
if errorlevel 1 (
    call :print_warning "MySQL 未安装，请确保数据库服务可用"
) else (
    call :print_message "MySQL 已安装"
)

:: 检查 Redis（可选）
redis-cli --version >nul 2>&1
if errorlevel 1 (
    call :print_warning "Redis 未安装，请确保 Redis 服务可用"
) else (
    call :print_message "Redis 已安装"
)
goto :eof

:: 安装依赖
:install_dependencies
call :print_message "安装 Go 依赖..."
go mod download
go mod tidy
goto :eof

:: 创建必要的目录
:create_directories
call :print_message "创建必要的目录..."
if not exist "logs" mkdir logs
if not exist "build" mkdir build
if not exist "docker\mysql" mkdir docker\mysql
if not exist "docker\nginx\ssl" mkdir docker\nginx\ssl
goto :eof

:: 检查配置文件
:check_config
call :print_message "检查配置文件..."
if not exist "config\config.yaml" (
    call :print_error "配置文件 config\config.yaml 不存在"
    exit /b 1
)
call :print_message "配置文件检查通过"
goto :eof

:: 构建应用
:build_application
call :print_message "构建应用..."
if "%1"=="release" (
    call :print_message "构建发布版本..."
    set CGO_ENABLED=0
    set GOOS=linux
    set GOARCH=amd64
    go build -a -installsuffix cgo -o build\pbj-server.exe main.go
) else (
    call :print_message "构建开发版本..."
    go build -o build\pbj-server.exe main.go
)
call :print_message "构建完成"
goto :eof

:: 运行应用
:run_application
call :print_message "启动应用..."
if exist "build\pbj-server.exe" (
    build\pbj-server.exe
) else (
    call :print_message "使用 go run 启动..."
    go run main.go
)
goto :eof

:: 使用 Docker Compose 启动
:start_with_docker
call :print_message "使用 Docker Compose 启动..."
docker-compose --version >nul 2>&1
if errorlevel 1 (
    call :print_error "Docker Compose 未安装"
    exit /b 1
)
docker-compose up -d
call :print_message "Docker 服务已启动"
call :print_message "应用地址: http://localhost:8080"
call :print_message "phpMyAdmin: http://localhost:8081"
call :print_message "Redis Commander: http://localhost:8082"
goto :eof

:: 停止 Docker 服务
:stop_docker
call :print_message "停止 Docker 服务..."
docker-compose down
call :print_message "Docker 服务已停止"
goto :eof

:: 显示帮助信息
:show_help
echo 用法: %0 [选项]
echo.
echo 选项:
echo   build [release]    构建应用 (可选: release 模式)
echo   run                运行应用
echo   dev                开发模式运行
echo   docker-up          使用 Docker Compose 启动
echo   docker-down        停止 Docker 服务
echo   test               运行测试
echo   clean              清理构建文件
echo   help               显示此帮助信息
echo.
echo 示例:
echo   %0 build           # 构建开发版本
echo   %0 build release   # 构建发布版本
echo   %0 run             # 运行应用
echo   %0 docker-up       # 启动 Docker 服务
goto :eof

:: 主函数
:main
call :print_header

if "%1"=="" goto :help
if "%1"=="help" goto :help
if "%1"=="build" goto :build
if "%1"=="run" goto :run
if "%1"=="dev" goto :dev
if "%1"=="docker-up" goto :docker_up
if "%1"=="docker-down" goto :docker_down
if "%1"=="test" goto :test
if "%1"=="clean" goto :clean
goto :help

:build
call :check_dependencies
call :install_dependencies
call :create_directories
call :check_config
call :build_application %2
goto :eof

:run
call :check_dependencies
call :install_dependencies
call :create_directories
call :check_config
call :run_application
goto :eof

:dev
call :check_dependencies
call :install_dependencies
call :create_directories
call :check_config
call :print_message "开发模式启动..."
go run main.go
goto :eof

:docker_up
call :start_with_docker
goto :eof

:docker_down
call :stop_docker
goto :eof

:test
call :print_message "运行测试..."
go test -v ./...
goto :eof

:clean
call :print_message "清理构建文件..."
go clean
if exist "build" rmdir /s /q build
if exist "pbj-server.exe" del pbj-server.exe
call :print_message "清理完成"
goto :eof

:help
call :show_help
goto :eof 